package com.peanut.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peanut.domain.bo.TabInfoBO;
import com.peanut.domain.po.TabInfoPO;
import com.peanut.domain.po.TabSpecInfoPO;
import com.peanut.mapper.TabSpecInfoMapper;
import com.peanut.service.TabInfoService;
import com.peanut.mapper.TabInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 张涛
* @description 针对表【tab_info】的数据库操作Service实现
* @createDate 2022-05-25 19:57:47
*/
@Service
public class TabInfoServiceImpl extends ServiceImpl<TabInfoMapper, TabInfoPO>
    implements TabInfoService{

    @Resource
    private TabInfoMapper infoMapper;

    @Resource
    private TabSpecInfoMapper specInfoMapper;


    @Override
    public int addInfo(TabInfoBO bo,Long specId) {
        TabInfoPO info = new TabInfoPO();
        BeanUtils.copyProperties(bo,info);
        int i = infoMapper.insertReturnId(info);
        TabSpecInfoPO specInfo = new TabSpecInfoPO();
        specInfo.setSpecId(specId);
        specInfo.setInfoId(info.getInfoId());
        specInfoMapper.insert(specInfo);
        return i;
    }

    @Override
    public Page<TabInfoPO> findAll(Integer pageNumber, Integer pageSize) {
        Page<TabInfoPO> page = new Page<>(pageNumber,pageSize);
        return infoMapper.selectPage(page,null);
    }

    @Override
    public int updateInfo(TabInfoBO bo) {
        TabInfoPO info = new TabInfoPO();
        BeanUtils.copyProperties(bo,info);
        return infoMapper.updateById(info);
    }

    @Override
    public int deleteInfo(Long infoId) {
        return infoMapper.deleteById(infoId);
    }

    @Override
    public List<TabInfoPO> getCatId(Long catId) {
        return infoMapper.selectList(new QueryWrapper<TabInfoPO>()
                .eq("cat_id",catId)
        );
    }
}




